#include "pencil.h"
#include "GraphicsArea/drawingarea.h"
using namespace std;

//************************************************************* Constructor

Pencil::Pencil(DrawingArea* drawingAreaUse,QDockWidget* dockInfoToolP) : ToolEffect(drawingAreaUse){
    dockInfoTool = dockInfoToolP;
    taille_Pencil = 1;
}

//************************************************************* Affichage of Dock Tool Info

void Pencil::createAffichage(){
    changeAffichage(1);
}

void Pencil::changeAffichage(int val){
    QWidget* widgetInfoToolUp = new QWidget();
    QWidget* widgetInfoTool = new QWidget(widgetInfoToolUp);
    QGridLayout* layoutInfoTool = new QGridLayout();

    QLabel* labelInfoGen;
    QToolButton* buttonTaillePencilPlus;
    QToolButton* buttonTaillePencilMoin;
    QSpinBox* spinTaillePencil;
    QAction* actionMoin;
    QAction* actionPlus;

    labelInfoGen = new QLabel();
    labelInfoGen->setObjectName(QString::fromUtf8("labelInfoGen"));
    labelInfoGen->setText(QString::fromUtf8("Outils Crayon   : Taille "));

    labelValPencil = new QLabel();
    labelValPencil->setObjectName(QString::fromUtf8("labelValPencil"));
    QString convert;
    labelValPencil->setText(convert.setNum(val));

    spinTaillePencil = new QSpinBox();
    spinTaillePencil->setObjectName(QString::fromUtf8("spinTaillePencil"));

    buttonTaillePencilPlus = new QToolButton();
    actionPlus = new QAction(QIcon("./image/add.png"), QString::fromUtf8("-"), buttonTaillePencilPlus);
    buttonTaillePencilPlus->setDefaultAction(actionPlus);

    buttonTaillePencilMoin = new QToolButton();
    actionMoin = new QAction(QIcon("./image/moin.png"), QString::fromUtf8("-"), buttonTaillePencilMoin);
    buttonTaillePencilMoin->setDefaultAction(actionMoin);

    layoutInfoTool->addWidget(labelInfoGen, 0, 0, Qt::AlignVCenter);
    layoutInfoTool->addWidget(buttonTaillePencilMoin, 0, 1, Qt::AlignVCenter);
    layoutInfoTool->addWidget(labelValPencil, 0, 2, Qt::AlignVCenter);
    layoutInfoTool->addWidget(buttonTaillePencilPlus, 0, 3, Qt::AlignVCenter);

    widgetInfoTool->setLayout(layoutInfoTool);
    dockInfoTool->setWidget(widgetInfoToolUp);
    QObject::connect(buttonTaillePencilPlus,SIGNAL(pressed()),this,SLOT(changetaillePencilPlus()));
    QObject::connect(buttonTaillePencilMoin,SIGNAL(pressed()),this,SLOT(changetaillePencilMoin()));
}

void Pencil::changetaillePencilPlus(){
    taille_Pencil++;
    changeAffichage(taille_Pencil);
}

void Pencil::changetaillePencilMoin(){
    if (taille_Pencil >=2){
        taille_Pencil--;
        changeAffichage(taille_Pencil);
    }
}

//************************************************************* Pencil mouvement

void Pencil::initPencil(int x, int y){
    if(painter == NULL){
        QImage  *image = drawingArea->getCurrentLayer()->getImage();
        painter = new QPainter(image);
        x_Begin = x - drawingArea->getCurrentLayer()->x();
        y_Begin = y - drawingArea->getCurrentLayer()->y();
    }
}

void Pencil::designer(int x, int y){
    QPen penn(Qt::green, taille_Pencil, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    painter->setPen(penn);

    painter->drawLine(x_Begin,y_Begin,x - drawingArea->getCurrentLayer()->x(),y- drawingArea->getCurrentLayer()->y());
    x_Begin = x - drawingArea->getCurrentLayer()->x();
    y_Begin = y - drawingArea->getCurrentLayer()->y();
}

void Pencil::endPainting(){
    delete painter;
    painter = NULL;
}
